package com.xu.tools.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author xuguan
 * @date 2023/3/1
 * @desc MybatisPlus代码生成器
 */
public class MybatisPlusGenerator {

    // 注释信息
    private static final String author;
    private static final String date;
    // 数据库相关
    private static final String url;
    private static final String username;
    private static final String password;
    // 需要自动生成的表名
    private static final List<String> tableNames = new ArrayList<>();
    // 代码生成目录
    private static final String outDir;

    static {
        tableNames.add("t_user");

        url = "jdbc:mysql://localhost:3306/table_content?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        username = "root";
        password = "123456";

        author = "xuguan";
        date = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(LocalDateTime.now());
        outDir = "/Users/guanxu/Downloads";
    }

    public static void generate() {
        FastAutoGenerator.create("url", "username", "password")
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("D://"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com") // 设置父包名
                            .moduleName("generate") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, outDir + "/xml")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("t_simple") // 设置需要生成的表名
                            .addTablePrefix("t_", "c_") // 设置过滤表前缀

                            .entityBuilder()
                            .enableRemoveIsPrefix() // 移除is前缀, 视情况开启
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .addTableFills(new Column("create_time", FieldFill.INSERT),
                                    new Column("updateTime", FieldFill.INSERT_UPDATE)) // mybatisplus自动注入
                            .versionColumnName("version_lock")// 乐观锁
                            .formatFileName("%sEntity")

                            .mapperBuilder()
                            .enableMapperAnnotation()
                            .formatMapperFileName("%sMapper")

                            .formatXmlFileName("%sMapper")
                    ;
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    public static void main(String[] args) {
        generate();
    }
}
